// DisplayView.cpp : implementation file
//

#include "stdafx.h"
#include "Bloodlead.h"
#include "MainFrm.h"
#include "DisplayView.h"

#include "BloodleadDoc.h"

// CDisplayView
#define BITMAP_WIDTH (1024*4)
#define BITMAP_HEIGHT (1024*4)

IMPLEMENT_DYNCREATE(CDisplayView, CScrollView)

CDisplayView::CDisplayView()
{
	mMouseDownPt = mMouseUpPt = CPoint(0,0);
	mIsInit = FALSE;
	mOffsetX = mOffsetY = 0;
	mSelectFlag = FALSE;
}

CDisplayView::~CDisplayView()
{
}

BEGIN_MESSAGE_MAP(CDisplayView, CScrollView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_MESSAGE( WM_USER + 10000, OnSamplingOver )
END_MESSAGE_MAP()


// CDisplayView drawing

void CDisplayView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = BITMAP_WIDTH;
	sizeTotal.cy = BITMAP_HEIGHT;
	SetScrollSizes(MM_TEXT, sizeTotal);

	if( !mIsInit )
	{
		CDC* pDC = GetDC();
		mBitmap.CreateCompatibleBitmap( pDC, BITMAP_WIDTH, BITMAP_HEIGHT );
		mDC.CreateCompatibleDC( pDC );
		mDC.SelectObject( mBitmap );

		Refresh();
		mIsInit = TRUE;

		ReleaseDC( pDC );
	}
	SetScrollPos( SB_VERT, BITMAP_HEIGHT );
	mOffsetX = 0;
	mOffsetY = BITMAP_HEIGHT;
}

CBloodleadDoc* CDisplayView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBloodleadDoc)));
	return (CBloodleadDoc*)m_pDocument;
}

void CDisplayView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here

	//CRect rc;
	//GetClientRect( rc );

	//pDC->BitBlt( mOffsetX, mOffsetY - rc.Height(), rc.Width(), rc.Height(),	&mDC, mOffsetX, mOffsetY - rc.Height(), SRCCOPY );

	CDC* pDrawDC = &mDC;

	// only paint the rect that needs repainting
	CRect rect;
	CPoint ScrollPos;
	pDC->GetClipBox( rect );
	if( !rect.IsRectEmpty() )
		int i = 0;
	//DocToClient(rect);

	CBrush brush;
	if (!brush.CreateSolidBrush( RGB( 170, 170, 170 ) ) )
		return;
	
	brush.UnrealizeObject();
	CSize size;
	CRect rc;
	GetClientRect( rc );

	pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
		pDrawDC, rect.left, rect.top, SRCCOPY);

	static int i = 0;
	TCHAR szOutput[1024];
	_sntprintf( szOutput, _countof(szOutput), _T("OnDraw(%d) OffsetX = %d OffsetY = %d\r\n"), i++, mOffsetX, mOffsetY );
	OutputDebugString( szOutput );
}

void CDisplayView::Refresh()
{
	static int i = 0;
	TCHAR szOutput[1024];
	_sntprintf( szOutput, _countof(szOutput), _T("Refresh(%d) OffsetX = %d OffsetY = %d\r\n"), i++, mOffsetX, mOffsetY );
	OutputDebugString( szOutput );

	CRect rect( 0, 0, BITMAP_WIDTH, BITMAP_HEIGHT );
	mDC.FillRect( &rect, &CBrush( RGB( 255, 255, 127 ) ) );

	CPen redPen( PS_SOLID, 1, RGB( 255, 0, 0 ) );
	CPen* pOldPen = mDC.SelectObject( &redPen );
	int nHeight = 0, nPosX = 0;

	CRect ClientRect;
	GetClientRect( ClientRect );
	mDC.MoveTo( 5, 0 );
	mDC.LineTo( 5, BITMAP_HEIGHT - 5 );
	for( INT y = BITMAP_HEIGHT - 5; y > 0 ; y -= 10 )
	{
		mDC.MoveTo( 5, y );
		mDC.LineTo( 10, y );
	}

	mDC.MoveTo( 5, BITMAP_HEIGHT - 5 );
	mDC.LineTo( BITMAP_WIDTH, BITMAP_HEIGHT - 5 );
	for( INT x = 5; x < BITMAP_WIDTH; x += 10 )
	{
		mDC.MoveTo( x, BITMAP_HEIGHT - 5 );
		mDC.LineTo( x, BITMAP_HEIGHT - 10 );
		//mDC.DrawText( str, rc, 
	}

	CBloodleadDoc* pDoc = GetDocument();
	CPen bulePen( PS_SOLID, 1, RGB( 0, 0, 255 ) );
	mDC.SelectObject( &bulePen );

	for( UINT nIndex = 0; nIndex < pDoc->ElcCount(); ++nIndex )
	{
		INT nPos = BITMAP_HEIGHT - 5 - (INT)(pDoc->ElcValue(nIndex)+0.5f);
		mDC.MoveTo( 10 * nIndex + 15, nPos );
		mDC.LineTo( 10 * nIndex + 15, BITMAP_HEIGHT - 5 );
	}

	CRect memRect( mMouseDownPt, mMouseUpPt );
	memRect.NormalizeRect();
	mDC.InvertRect( memRect );

	mDC.SelectObject( pOldPen );
}

// CDisplayView diagnostics

#ifdef _DEBUG
void CDisplayView::AssertValid() const
{
	CScrollView::AssertValid();
}

#ifndef _WIN32_WCE
void CDisplayView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDisplayView message handlers

void CDisplayView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if( mSelectFlag )
	{
		CDC* pDC = GetDC();

		CRect OldRect( mMouseDownPt - CSize( mOffsetX, mOffsetY ), mMouseUpPt - CSize( mOffsetX, mOffsetY ) );
		OldRect.NormalizeRect();
		pDC->InvertRect( OldRect );

		CRect memRect( mMouseDownPt, mMouseUpPt );
		memRect.NormalizeRect();
		mDC.InvertRect( memRect );

		mMouseDownPt = mMouseUpPt = point + CSize( mOffsetX, mOffsetY );

		CRect NewRect( mMouseDownPt - CSize( mOffsetX, mOffsetY ), mMouseUpPt - CSize( mOffsetX, mOffsetY ) );
		NewRect.NormalizeRect();
		pDC->DrawFocusRect( NewRect );

		mBegin = (mMouseDownPt.x - 5) / 10;
		ReleaseDC( pDC );

		SetCapture();
	}
	CScrollView::OnLButtonDown(nFlags, point);
}

void CDisplayView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if( mSelectFlag )
	{
		ReleaseCapture();

		CDC* pDC = GetDC();

		CRect OldRect( mMouseDownPt - CSize( mOffsetX, mOffsetY ), mMouseUpPt - CSize( mOffsetX, mOffsetY ) );
		OldRect.NormalizeRect();
		pDC->DrawFocusRect( OldRect );
		pDC->InvertRect( OldRect );

		CRect memRect( mMouseDownPt, mMouseUpPt );
		memRect.NormalizeRect();
		mDC.InvertRect( memRect );

		mMouseUpPt = point + CSize( mOffsetX, mOffsetY );
		ReleaseDC( pDC );

		mEnd = (mMouseUpPt.x - 5) / 10 - 1;

		if( mEnd < mBegin )
		{
			INT Temp = mBegin;
			mBegin = mEnd;
			mEnd = Temp;
		}

		INT nAnswer = AfxMessageBox( _T("are you want using current sampling area?"), MB_YESNO );
		if( nAnswer != IDYES )
		{
			mSelectFlag = TRUE;
			return CScrollView::OnLButtonUp(nFlags, point);
		}

		mSelectFlag = FALSE;

		CBloodleadDoc* pDoc = GetDocument();
		if( pDoc )
		{
			pDoc->SetRecordset( pDoc->Index(), pDoc->CalculateX( mBegin, mEnd ), pDoc->GetElcValueY(pDoc->Index()) );
			pDoc->SetRecordFlag( pDoc->Index(), TRUE );
			pDoc->UpdateAllViews( NULL );

			CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
			if( pMain )
			{
				pMain->NextSampling();
			}

		}
	}
	CScrollView::OnLButtonUp(nFlags, point);
}

void CDisplayView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if( mSelectFlag && GetCapture() == this )
	{
		CRect rcClient;
		GetClientRect( rcClient );

		CDC* pDC = GetDC();

		CRect OldRect( mMouseDownPt - CSize( mOffsetX, mOffsetY ), mMouseUpPt - CSize( mOffsetX, mOffsetY ) );
		OldRect.NormalizeRect();
		pDC->DrawFocusRect( OldRect );

		TCHAR szOutput[1024];
		_sntprintf( szOutput, _countof(szOutput), _T("Move(%d,%d) OffsetX = %d OffsetY = %d\r\n"), point.x, point.y, mOffsetX, mOffsetY );
		OutputDebugString( szOutput );

		if( point.x > rcClient.right - max( 10, rcClient.Width()*5/100 ) )
		{
			SetScrollPos( SB_HORZ, mOffsetX + 10 );
			mOffsetX = GetScrollPos( SB_HORZ );
		}
		else if( point.x < rcClient.left + max( 10, rcClient.Width()*5/100 ) )
		{
			SetScrollPos( SB_HORZ, mOffsetX - 10 );
			mOffsetX = GetScrollPos( SB_HORZ );
		}

		if( point.y < rcClient.top + max( 10, rcClient.Height()*5/100 ) )
		{
			SetScrollPos( SB_VERT, mOffsetY - 10 );
			mOffsetY = GetScrollPos( SB_VERT );
		}
		else if( point.y > rcClient.bottom - max( 10, rcClient.Height()*5/100 ) )
		{
			SetScrollPos( SB_VERT, mOffsetY + 10 );
			mOffsetY = GetScrollPos( SB_VERT );
		}

		mMouseUpPt = point + CSize( mOffsetX, mOffsetY );
		CRect NewRect( mMouseDownPt - CSize( mOffsetX, mOffsetY ), mMouseUpPt - CSize( mOffsetX, mOffsetY ) );
		NewRect.NormalizeRect();
		pDC->DrawFocusRect( NewRect );

		ReleaseDC( pDC );
	}
	CScrollView::OnMouseMove(nFlags, point);
}

void CDisplayView::OnSize(UINT nType, int cx, int cy)
{
	CScrollView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	//CBitmap* pBitmap = new CBitmap();
	//pBitmap->CreateCompatibleBitmap( &mDC, cx, cy );
	//CBitmap* pOldBitmap = mDC.SelectObject( pBitmap );

	//delete pOldBitmap;
	//mBitmap = pBitmap;

	//CRect rect( 0, 0, cx, cy );
	//mDC.FillRect( &rect, &CBrush( RGB( 0, 64, 0 ) ) );
	//if( mIsInit )
	//	Refresh();
}

BOOL CDisplayView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
}

LRESULT CDisplayView::OnSamplingOver( WPARAM wParam, LPARAM lParam )
{
	INT nAnswer = AfxMessageBox( _T("are you want using current sampling area?"), MB_YESNO );
	if( nAnswer != IDYES )
	{
		mSelectFlag = TRUE;
		return 0;
	}

	CBloodleadDoc* pDoc = GetDocument();
	if( pDoc )
	{
		if( wParam == 0 )
		{
			pDoc->SetRecordset( pDoc->Index(), pDoc->CalculateX( mBegin, mEnd ), pDoc->GetElcValueY(pDoc->Index()) );
			pDoc->SetRecordFlag( pDoc->Index(), TRUE );
			pDoc->UpdateAllViews( NULL );

			CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd();
			if( pMain )
			{
				pMain->NextSampling();
			}
		}
		else
			pDoc->CalculateY( pDoc->Index(), pDoc->CalculateX( mBegin, mEnd ) );
	}
	return 0;
}
